// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <string>
#include <pollux/expression/vector_function.h>
#include <pollux/functions/registerer.h>
#include <pollux/functions/lib/map_concat.h>
#include <pollux/functions/prestosql/map.h>
#include <pollux/functions/prestosql/map_functions.h>
#include <pollux/functions/prestosql/map_keys_by_top_n_values.h>
#include <pollux/functions/prestosql/map_normalize.h>
#include <pollux/functions/prestosql/map_subset.h>
#include <pollux/functions/prestosql/map_top_n.h>
#include <pollux/functions/prestosql/map_top_n_keys.h>
#include <pollux/functions/prestosql/map_top_n_values.h>
#include <pollux/functions/prestosql/multimap_from_entries.h>

namespace kumo::pollux::functions {
    namespace {
        template<typename T>
        void registerMapSubsetPrimitive(const std::string &prefix) {
            register_function<
                ParameterBinder<MapSubsetPrimitiveFunction, T>,
                Map<T, Generic<T1> >,
                Map<T, Generic<T1> >,
                Array<T> >({prefix + "map_subset"});
        }

        void registerMapSubset(const std::string &prefix) {
            registerMapSubsetPrimitive<bool>(prefix);
            registerMapSubsetPrimitive<int8_t>(prefix);
            registerMapSubsetPrimitive<int16_t>(prefix);
            registerMapSubsetPrimitive<int32_t>(prefix);
            registerMapSubsetPrimitive<int64_t>(prefix);
            registerMapSubsetPrimitive<float>(prefix);
            registerMapSubsetPrimitive<double>(prefix);
            registerMapSubsetPrimitive<Timestamp>(prefix);
            registerMapSubsetPrimitive<Date>(prefix);

            register_function<
                MapSubsetVarcharFunction,
                Map<Varchar, Generic<T1> >,
                Map<Varchar, Generic<T1> >,
                Array<Varchar> >({prefix + "map_subset"});

            register_function<
                MapSubsetFunction,
                Map<Generic<T1>, Generic<T2> >,
                Map<Generic<T1>, Generic<T2> >,
                Array<Generic<T1> > >({prefix + "map_subset"});
        }

        void registerMapRemoveNullValues(const std::string &prefix) {
            register_function<
                MapRemoveNullValues,
                Map<Generic<T1>, Generic<T2> >,
                Map<Generic<T1>, Generic<T2> > >({prefix + "map_remove_null_values"});
        }

        void registerMapKeyExists(const std::string &prefix) {
            register_function<
                MapKeyExists,
                bool,
                Map<Generic<T1>, Generic<T2> >,
                Generic<T1> >({prefix + "map_key_exists"});
        }
    } // namespace

    void registerMapFunctions(const std::string &prefix) {
        POLLUX_REGISTER_VECTOR_FUNCTION(udf_map_filter, prefix + "map_filter");
        POLLUX_REGISTER_VECTOR_FUNCTION(udf_transform_keys, prefix + "transform_keys");
        POLLUX_REGISTER_VECTOR_FUNCTION(
            udf_transform_values, prefix + "transform_values");
        registerMapFunction(prefix + "map", false /*allowDuplicateKeys*/);
        POLLUX_REGISTER_VECTOR_FUNCTION(udf_map_entries, prefix + "map_entries");
        POLLUX_REGISTER_VECTOR_FUNCTION(
            udf_map_from_entries, prefix + "map_from_entries");
        POLLUX_REGISTER_VECTOR_FUNCTION(udf_map_keys, prefix + "map_keys");
        POLLUX_REGISTER_VECTOR_FUNCTION(udf_map_values, prefix + "map_values");
        POLLUX_REGISTER_VECTOR_FUNCTION(udf_map_zip_with, prefix + "map_zip_with");

        POLLUX_REGISTER_VECTOR_FUNCTION(udf_all_keys_match, prefix + "all_keys_match");
        POLLUX_REGISTER_VECTOR_FUNCTION(udf_any_keys_match, prefix + "any_keys_match");
        POLLUX_REGISTER_VECTOR_FUNCTION(udf_no_keys_match, prefix + "no_keys_match");

        POLLUX_REGISTER_VECTOR_FUNCTION(
            udf_any_values_match, prefix + "any_values_match");
        POLLUX_REGISTER_VECTOR_FUNCTION(
            udf_no_values_match, prefix + "no_values_match");

        registerMapConcatFunction(prefix + "map_concat");

        register_function<
            MultimapFromEntriesFunction,
            Map<Generic<T1>, Array<Generic<T2> > >,
            Array<Row<Generic<T1>, Generic<T2> > > >({prefix + "multimap_from_entries"});

        register_function<
            MapTopNFunction,
            Map<Orderable<T1>, Orderable<T2> >,
            Map<Orderable<T1>, Orderable<T2> >,
            int64_t>({prefix + "map_top_n"});

        register_function<
            MapTopNKeysFunction,
            Array<Orderable<T1> >,
            Map<Orderable<T1>, Generic<T2> >,
            int64_t>({prefix + "map_top_n_keys"});

        register_function<
            MapKeysByTopNValuesFunction,
            Array<Orderable<T1> >,
            Map<Orderable<T1>, Orderable<T2> >,
            int64_t>({prefix + "map_keys_by_top_n_values"});

        register_function<
            MapTopNValuesFunction,
            Array<Orderable<T2> >,
            Map<Orderable<T1>, Orderable<T2> >,
            int64_t>({prefix + "map_top_n_values"});

        registerMapSubset(prefix);

        registerMapRemoveNullValues(prefix);

        registerMapKeyExists(prefix);

        register_function<
            MapNormalizeFunction,
            Map<Varchar, double>,
            Map<Varchar, double> >({prefix + "map_normalize"});
    }

    void registerMapAllowingDuplicates(
        const std::string &name,
        const std::string &prefix) {
        POLLUX_REGISTER_VECTOR_FUNCTION(udf_map_allow_duplicates, prefix + name);
    }
} // namespace kumo::pollux::functions
